package net.zjitc.controller;

import java.util.List;
import net.zjitc.pojo.Category;
import net.zjitc.pojo.CategorySecond;
import net.zjitc.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.InternalResourceView;
import org.springframework.web.servlet.view.RedirectView;

/**
 * Create by IntelliJ IDEA
 *
 * @author: jsonor
 * @date-Time: 2018/4/28 16:10
 * @description:
 */
//列表相关
@Controller
public class AdminCategory {
  @Autowired
  private CategoryService categoryService;

//显示菜单列表(一级)
  @RequestMapping("/adminCategory")
  public ModelAndView adminCategory(){
    List<Category> list=categoryService.findAll();
    ModelAndView mv=new ModelAndView();
    mv.addObject("lists",list);
    mv.setView(new InternalResourceView("shelll68/one/onelist.jsp"));
    return mv;
  }

//显示菜单列表(二级)
  @RequestMapping("/adminCategorySecond")
  public ModelAndView adminCategorySecond(){
    List<CategorySecond> list=categoryService.findAllSecond();
    ModelAndView mv=new ModelAndView();
    mv.addObject("lists",list);
    mv.setView(new InternalResourceView("/shelll68/second/onelist.jsp"));
    return mv;
  }

//添加页面显示(一级)
  @RequestMapping("/addUICaterory")
  public ModelAndView addUICaterory(){
    ModelAndView mv =new ModelAndView();
    mv.setView(new RedirectView("/shelll68/one/add.jsp"));
    return mv;
  }

//添加页面显示(二级)
  @RequestMapping("/addUICategorySecond")
  public ModelAndView addUICategorySecond(){
    ModelAndView modelAndView =new ModelAndView();
    List<Category> list=categoryService.findAll();
    modelAndView.addObject("clist",list);
    modelAndView.setView(new InternalResourceView("/shelll68/second/add.jsp"));
    return  modelAndView;
  }

//添加(一级)
  @RequestMapping("/addCategory")
  public ModelAndView addCategory(
      @RequestParam(name = "cname") String cname
  ){
    ModelAndView mv = new ModelAndView();
    Integer count=categoryService.addCategory(cname);
    mv.setView(new InternalResourceView("/adminCategory.do"));
    return mv;
  }

//添加(二级)
  @RequestMapping("/addCategorySecond")
  public ModelAndView addCategorySecond(
      Category category,
      CategorySecond categorySecond
  ){
    ModelAndView mv =new ModelAndView();
    categorySecond.setCid(category.getCid());
    Integer count=categoryService.addCategorySecond(categorySecond);
    mv.setView(new RedirectView("/adminCategorySecond.do"));
    return mv;
  }

//回显菜单列表(一级)
  @RequestMapping("/editUICategory")
  public ModelAndView editUICategory(
      @RequestParam(name = "cid") Integer cid
  ){
    ModelAndView mv = new ModelAndView();
    Category category=categoryService.findByCid(cid);
    mv.addObject("c",category);
    mv.setView(new InternalResourceView("/shelll68/one/edit.jsp"));
    return mv;
  }
  //回显菜单列表(二级)
  @RequestMapping("/editUICategorySecond")
  public ModelAndView editUICategorySecond(
      @RequestParam(name = "csid") Integer csid
  ) {
    ModelAndView mv =new ModelAndView();
    CategorySecond categorySecond =categoryService.findByCsid(csid);
    mv.addObject("cs", categorySecond);
    List<Category> list =categoryService.findAll();
    mv.addObject("clist", list);
    mv.setView(new InternalResourceView("/shelll68/second/edit.jsp"));
    return mv;
  }

//修改菜单列表(一级)
  @RequestMapping("/editCategory")
  public ModelAndView editCategory(Category category
      ){
   ModelAndView mv = new ModelAndView();
     Integer abc = categoryService.editCategory(category);
    mv.setView(new InternalResourceView("/adminCategory.do"));
    return mv;
  }

//修改菜单列表(二级)
@RequestMapping("/editCategorySecond")
public ModelAndView editCategorySecond(CategorySecond categorySecond,Category category){
    ModelAndView mv =new ModelAndView();
    categorySecond.setCid(category.getCid());
    Integer count=  categoryService.editCategorySecond(categorySecond);
    mv.setView(new InternalResourceView("/adminCategorySecond.do"));
    return mv;
  }

//删除(一级)
  @RequestMapping("/deleteCategory")
  public ModelAndView deleteCategory(
      @RequestParam(name = "cid") String cid
  ){
    ModelAndView mv = new ModelAndView();
    Integer count=categoryService.deleteCategory(cid);
    mv.setView(new InternalResourceView("/adminCategory.do"));
    return mv;
  }

//删除(二级)
  @RequestMapping("/deleteCategorySecond")
  public ModelAndView deleteCategorySecond(
      @RequestParam(name = "csid") String csid
  ){
    ModelAndView mv =new ModelAndView();
    Integer count=categoryService.deleteCategorySecond(csid);
    mv.setView(new RedirectView("/adminCategorySecond.do"));
    return mv;
  }
}
